const shop = require('../../providers/dataAccess').shop;

const CartService = function () {
  const _cartList = async (appId,userId,offset,limit) => {
     let szSql = "SELECT uc.id,productId,skuId,selectedNumber,selectedAmount,productName,thumbnail,unit FROM user_carts AS uc LEFT JOIN products AS p ON uc.productId = p.id WHERE uc.appId = ? AND userId = ? ORDER BY uc.addTime DESC LIMIT ?,?";
     let rows = await shop.query(szSql,[appId,userId,offset,limit]);
     return rows;
  };

  const _addCart = async (appId,userId,productId,skuId,selectedNumber,selectedAmount) => {
    let _isExist = async (appId,userId,productId) => {
      let szSql = "SELECT id FROM user_carts WHERE appId = ? AND userId = ? AND productId = ? AND skuId = ?";
      let rows =  await shop.query(szSql,[appId,userId,productId,skuId]);
      return rows && rows.length ? true : false;
    }

    let _updateCart = async (appId,userId,productId,skuId,selectedAmount) => {
      let szSql = "UPDATE user_carts SET selectedNumber = selectedNumber + 1,selectedAmount = ? WHERE appId = ? AND userId = ? AND productId = ? AND skuId = ?";
      let ret = await shop.query(szSql,[selectedAmount,appId,userId,productId,skuId]);
      return ret.changedRows > 0 ? true : false;
    }

    let _insertCart = async (appId,userId,productId,skuId,selectedNumber,selectedAmount) => {
      let szSql = "INSERT user_carts(appId,userId,productId,skuId,selectedNumber,selectedAmount,addTime) values (?,?,?,?,?,?,?)";
      let ret = await shop.query(szSql,[appId,userId,productId,skuId,selectedNumber,selectedAmount,Math.floor(new Date().getTime() / 1000)]);
      return ret.insertId > 0 ? true : false;
    }

    let isExist = await _isExist(appId,userId,productId);
    if (isExist) {
       return _updateCart(appId,userId,productId,skuId,selectedAmount);
    } else {
       return _insertCart(appId,userId,productId,skuId,selectedNumber,selectedAmount);
    }
    return false;
  };

  const _updateCount = async (userId,productId,skuId,type) => {
      let count = type == 2 ? 1 : -1;
      console.log(count);
      let szWhere = type == 0 ? "" : " AND selectedNumber > 1";
      let szSql = "UPDATE user_carts SET selectedNumber = selectedNumber + ? WHERE userId = ? AND productId = ? AND skuId = ?" + szWhere;
      let ret = await shop.query(szSql,[count,userId,productId,skuId]);
      console.log(ret);
      return ret.changedRows > 0 ? true : false;
  };

  const _deleteCart = async (userId,productId,skuId) => {
      let skuIdArr = skuIds == "" ? [] : skuIds.split(",").map(function (val) {
          return Number(val);
      });
      let szSql = "DELETE FROM user_carts WHERE userId = ? AND productId = ? AND skuId (?)";
      let ret = await shop.query(szSql,[userId,productId,skuId]);
      return ret.affectedRows > 0 ? true : false;
  };

  const _getSelectCart = async (appId,userId,skuIds) => {
    console.log("skuIds:" + skuIds);
    let skuIdArr = skuIds == "" ? [] : skuIds.split(",");
    let szSql = "SELECT uc.id,productId,skuId,selectedNumber,selectedAmount,productName,thumbnail,unit FROM user_carts AS uc LEFT JOIN products AS p ON uc.productId = p.id WHERE uc.appId = ? AND userId = ? AND skuId in (?)";
    let rows = await shop.query(szSql,[appId,userId,skuIdArr]);
    return rows;
  }

  const _clearCart = async (appId,userId,skuIds) => {
    let skuIdArr = skuIds == "" ? [] : skuIds.split(",").map(function (val) {
        return Number(val);
    });
    let szSql = "DELETE FROM user_carts WHERE appId = ? AND userId = ? AND skuId in (?)";
    let ret = await shop.query(szSql,[appId,userId,skuIdArr]);
    return ret.affectedRows > 0 ? true : false;
  }

  return {
    list: _cartList,
    add: _addCart,
    update: _updateCount,
    del: _deleteCart,
    getSelectCart: _getSelectCart,
    clearCart: _clearCart
  }
}();

module.exports = CartService;
